﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using TelecoSystems.Infrastructure.Helpers;
using TelecoSystems.Web.Models.Shared;
using TelecoSystems.Web.Services.Dashboard;
using System.ServiceModel.DomainServices.Client;
using TelecoSystems.Infrastructure.Views;

namespace TelecoSystems.Modules.Dashboard.ViewModels
{
    public class DashboardViewModel : ViewModelBase {
        private DashboardDomainContext _dashboardCtx;
        
        #region Constructors
        public DashboardViewModel() {
            _dashboardCtx = new DashboardDomainContext();

            ChartPointsCOP = new ObservableCollection<ChartPointItem>();
            ChartPointsCL = new ObservableCollection<ChartPointItem>();            

            _dashboardCtx.Load(_dashboardCtx.GetContractsOverProductsQuery(), ContractsOverProductsLoaded, null);
            
            ChartPoints3 = new ObservableCollection<ChartPointItem>();
            for (int i = 25; i < 35; i++) {
                ChartPoints3.Add(new ChartPointItem() { Value = i * 10, Label = (i - 24).ToString() });
            }
            NotifyPropertyChanged("ChartPoints3");
        }
        #endregion

        #region VM Properties
        public string HeaderInfo {
            get {
                return "Dashboard";
            }
        }

        public ObservableCollection<ChartPointItem> ChartPointsCOP { get; set; }
        public ObservableCollection<ChartPointItem> ChartPointsCL { get; set; }
        public ObservableCollection<ChartPointItem> ChartPoints3 { get; set; }
        #endregion    
        
        protected void ContractsOverProductsLoaded(LoadOperation<ChartPointItem> loadOperation) {
            if (loadOperation.HasError) {
                ErrorWindow.CreateNew(loadOperation.Error);
            } else {
                foreach (ChartPointItem item in loadOperation.Entities) {
                    ChartPointsCOP.Add(item);
                }
                NotifyPropertyChanged("ChartPointsCOP");

                _dashboardCtx.Load(_dashboardCtx.GetCustomersLocationQuery(), CustomersLocationLoaded, null);
            }
        }

        protected void CustomersLocationLoaded(LoadOperation<ChartPointItem> loadOperation) {
            if (loadOperation.HasError) {
                ErrorWindow.CreateNew(loadOperation.Error);
            } else {
                foreach (ChartPointItem item in loadOperation.Entities) {
                    ChartPointsCL.Add(item);
                }
                NotifyPropertyChanged("ChartPointsCL");                
            }
        }
    }    
}
